Welcome back.
We're virtually finished with our project, but there's just one last thing that I'd like for us to
fix in our SCP.
Now, if you remember, in our server side drink class, when we set the owner for a particular drink
right here, we're connecting a function to when the player dies and when that player dies, we go ahead
and go and destroy any drink objects that are in their inventory.
Now, there is a very slight problem with this system, and that's because we're not disconnecting this
event on our character for drinks that may have been destroyed already.
So I'm going to show you a very good example of this problem.
Let's say I go to my drink machine and I go and get a cup of air.
And let's say I consume it and it gets destroyed.
And let's go ahead and say I get another drink, maybe a cup of pain.
And I go ahead and drink it, and that kind of fun stuff happens.
Now, if I go and reset my character, you're going to see an error pop up in the console and you're
going to see it right here in our server drink class, attempt to call Myscene method destroy of table.
And if we trace this back to our drink class, we're getting our error message right here.
And it's inside of our on owner death function.
And that's because our error cup got destroyed previously, but we never disconnected the function attached
to the died event on our character.
So that's a problem we need to fix.
And to fix that I'm going to create another table inside of our server drink class.
I'm going to call it connections.
And this is going to store a table full of all of the connections for specific drinks in our game.
So let's go ahead and go down to the set owner function here.
And what I want to do is I want to check if there is not a table that exists for this particular drink
inside of our connections table.
So if there is not in our connections table, a key value pair for this particular drink with this ID,
then we need to create one.
So inside of our connections we're going to create a new key value pair using the ID of this drink.
And it's going to be set to an empty table.
And then inside of this table we can define some key value pairs that are going to store some events
or connections.
So right here when we connect this function to this event it's going to return back to us I believe
it's called an RRB script connection.
And we need to store that inside of our connections table so we can do connections.
Access the self.id table.
And inside of this table we'll create a new key value pair.
I'm just going to call it Owner Death.
And it's going to be equal to the R script connection that gets returned from this connect function.
So now that means when this drink object gets destroyed, we need to disconnect any possible connections
that this particular drink with this ID had.
So let's go ahead and go to our destroy function.
So destroy drink object.
What we need to do here is we need to loop through our connections table.
So what I could do is I could check if connections has a table for this particular drink with this ID.
If it does, then what we could do is we could loop through every single connection in pairs.
Connections.
self.id.
So we're looping through all of the key value pairs and make sure not to use I pairs, but pairs.
And what we could do with this connection is we could disconnect it.
And to make this more clear, I can denote this right here using type annotation as an RGB script connection
here.
So it's a connection between an RGB script signal and a function.
And we disconnect it.
And then once we've disconnected all of those functions then we can set the connections self.id equal
to nil.
So we're destroying this table at this key value pair.
So that means our problem should be resolved.
So if I go ahead.
And get a air drink.
And we go ahead and drink it and the cup gets destroyed when I go and reset, we shouldn't have that
error in the console anymore.
And there we go.
Our error is gone.
Other than that, our project is completed.
I've attached the completed SCP model to the lecture, including one that contains a lot of fun drinks
if you'd like to mess around with it.
Otherwise, congratulations finishing this project and I'll see you in the next lecture.